package com.example.ccj.u.media;

import android.content.Context;

import com.luck.picture.lib.basic.PictureSelectionModel;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;
import com.yalantis.ucrop.UCrop;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片加载工具  可以绘制水印
 * 如果要绘制水印要先设置waterMark变量的值
 */
public class MediaUtils {

    //图片水印文字
    private List<PhotoWaterMarkText> waterMark;

    //设置水印文字
    public MediaUtils setWaterMark(List<PhotoWaterMarkText> waterMark) {
        this.waterMark = waterMark;
        return this;
    }

    /**
     * 大图展示图片
     * @param context 上下文
     * @param currentPosition 当前显示的位置
     * @param isDisplayDelete 是否可删除
     * @param list 显示的数据
     */
    public void openPreview(Context context, int currentPosition, boolean isDisplayDelete, ArrayList<LocalMedia> list){
        PictureSelector.create(context)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
                .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                    @Override
                    public void onPreviewDelete(int position) {

                    }

                    @Override
                    public boolean onLongPressDownload(Context context, LocalMedia media) {
                        return false;
                    }
                })
                .isPreviewFullScreenMode(true)
                .isHidePreviewDownload(false)
                .startActivityPreview(currentPosition, isDisplayDelete, list);
    }

    /**
     * 单独使用相机
     * @param context 上下文
     * @param onResultCallbackListener 回调
     */
    public void camera(Context context, IPictureSelectResultListener onResultCallbackListener){
        PictureSelector.create(context)
                .openCamera(SelectMimeType.ofImage())
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true) //是否可以开启前置摄像头
                .forResult(onResultCallbackListener);
    }

    public void pickAPicture(Context context, List<LocalMedia> localMediaList, IPictureSelectResultListener onResultCallbackListener){
        PictureSelectionModel selectionModel = PictureSelector.create(context)
                .openGallery(SelectMimeType.ofImage())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setSelectionMode(1) //单选or多选
                .isDirectReturnSingle(true) //单选时立即返回
                .setMaxSelectNum(1)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(false)//是否显示相机按钮
                .setSelectMaxFileSize(1024*15)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true)//是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()));
        eeee(context,selectionModel);
        selectionModel.forResult(onResultCallbackListener);

    }

    public void pickAPictureOrCamera(Context context, List<LocalMedia> localMediaList, IPictureSelectResultListener onResultCallbackListener){
        PictureSelectionModel selectionModel = PictureSelector.create(context)
                .openGallery(SelectMimeType.ofImage())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setSelectionMode(1) //单选or多选
                .isDirectReturnSingle(true) //单选时立即返回
                .setMaxSelectNum(1)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(true)//是否显示相机按钮
                .setSelectMaxFileSize(1024*15)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true)//是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()));
        eeee(context,selectionModel);
        selectionModel.forResult(onResultCallbackListener);
    }


    public void pickMultiplePicture(Context context, List<LocalMedia> localMediaList,int pickNum, IPictureSelectResultListener onResultCallbackListener){
        PictureSelectionModel selectionModel = PictureSelector.create(context)
                .openGallery(SelectMimeType.ofImage())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setMaxSelectNum(pickNum)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(false)//是否显示相机按钮
                .setSelectMaxFileSize(1024*15)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true)//是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()));
        eeee(context,selectionModel);
        selectionModel.forResult(onResultCallbackListener);
    }

    public void pickMultiplePictureOrCamera(Context context, List<LocalMedia> localMediaList,int pickNum, IPictureSelectResultListener onResultCallbackListener){
        PictureSelectionModel selectionModel = PictureSelector.create(context)
                .openGallery(SelectMimeType.ofImage())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setMaxSelectNum(pickNum)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(true)//是否显示相机按钮
                .setSelectMaxFileSize(1024*15)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true) //是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()));
        eeee(context,selectionModel);
        selectionModel.forResult(onResultCallbackListener);
    }

    /**
     * 选择一个视频或图像文件
     * @param context 上下文
     * @param localMediaList 已选择文件
     */
    public void pickAMedia(Context context, List<LocalMedia> localMediaList, IPictureSelectResultListener onResultCallbackListener){
        PictureSelectionModel selectionModel = PictureSelector.create(context)
                .openGallery(SelectMimeType.ofAll())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setSelectionMode(1) //单选or多选
                .isDirectReturnSingle(true) //单选时立即返回
                .setMaxSelectNum(1)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(false)//是否显示相机按钮
                .setSelectMaxFileSize(1024*15)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true)//是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()));
        eeee(context,selectionModel);
        selectionModel.forResult(onResultCallbackListener);
    }

    /**
     * 选择多个视频或图像文件
     * @param context 上下文
     * @param localMediaList 已选择文件
     * @param pickNum 选择数量
     */
    public void pickMultipleMedia(Context context, List< LocalMedia> localMediaList,int pickNum, IPictureSelectResultListener onResultCallbackListener){
        PictureSelectionModel selectionModel = PictureSelector.create(context)
                .openGallery(SelectMimeType.ofAll())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setMaxSelectNum(pickNum)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(true)//是否显示相机按钮
                .setSelectMaxFileSize(1024*15)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true)//是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()))
                ;
        eeee(context,selectionModel);
        selectionModel.forResult(onResultCallbackListener);
    }


    private void eeee(Context context,PictureSelectionModel selectionModel){
        if (waterMark != null) {
            selectionModel.setAddBitmapWatermarkListener(new MeBitmapWatermarkEventListener(context,waterMark));//自定义水印
        }
    }

    /**
     * 选择一个视频文件
     * @param context 上下文
     * @param localMediaList 已选择文件
     */
    public void pickAVideo(Context context, List<LocalMedia> localMediaList, IPictureSelectResultListener onResultCallbackListener){
        PictureSelector.create(context)
                .openGallery(SelectMimeType.ofVideo())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setSelectionMode(1) //单选or多选
                .isDirectReturnSingle(true) //单选时立即返回
                .setMaxSelectNum(1)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(false)//是否显示相机按钮
                .setSelectMaxFileSize(1024*15)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true)//是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()))
                .forResult(onResultCallbackListener);
    }

    /**
     * 选择多个视频文件
     * @param context 上下文
     * @param localMediaList 已选择文件
     * @param pickNum 选择数量
     */
    public void pickMultipleVideo(Context context, List<LocalMedia> localMediaList,int pickNum, IPictureSelectResultListener onResultCallbackListener){
        PictureSelector.create(context)
                .openGallery(SelectMimeType.ofVideo())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setMaxSelectNum(pickNum)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(true)//是否显示相机按钮
                .setSelectMaxFileSize(1024*100)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true)//是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()))
                .forResult(onResultCallbackListener);
    }

    /**
     * 选择一个音频文件
     * @param context 上下文
     * @param localMediaList 已选择文件
     */
    public void pickAAudio(Context context, List<LocalMedia> localMediaList, IPictureSelectResultListener onResultCallbackListener){
        PictureSelector.create(context)
                .openGallery(SelectMimeType.ofVideo())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setSelectionMode(1) //单选or多选
                .isDirectReturnSingle(true) //单选时立即返回
                .setMaxSelectNum(1)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(false)//是否显示相机按钮
                .setSelectMaxFileSize(1024*30)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(false) //拍照是否纠正旋转图片
                .isCameraAroundState(false)//是否可以开启前置摄像头
                .setEditMediaInterceptListener(new MeOnMediaEditInterceptListener(context, new UCrop.Options()))
                .forResult(onResultCallbackListener);
    }

    /**
     * 选择多个视音频文件
     * @param context 上下文
     * @param localMediaList 已选择文件
     * @param pickNum 选择数量
     */
    public void pickMultipleAudio(Context context, List<LocalMedia> localMediaList,int pickNum, IPictureSelectResultListener onResultCallbackListener){
        PictureSelector.create(context)
                .openGallery(SelectMimeType.ofVideo())//显示文件类型
                .setSelectorUIStyle(MediaSelectorStyle.weCharStyle(context))//样式
                .setImageEngine(GlideEngine.createGlideEngine())
                .setMaxSelectNum(pickNum)//最大选择数量
                .setSelectedData(localMediaList)//已经选择的数据
                .isDisplayCamera(true)//是否显示相机按钮
                .setSelectMaxFileSize(1024*15)//最大选择的文件大小为15MB
                .isFastSlidingSelect(true)//快速滑动选择
                .isCameraRotateImage(true) //拍照是否纠正旋转图片
                .isCameraAroundState(true);//是否可以开启前置摄像头
    }


}
